<!--
 * @Author: jswang
 * @Date: 2021-07-29 10:30:16
 * @LastEditTime: 2021-08-16 09:40:02
-->
<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 放置内容 -->
      <el-card>
        <el-tabs @tab-click="handleClick" :value="activeName">
          <el-tab-pane label="角色管理" name="left">
            <el-row style="height:60px">
              <el-button
                type="primary"
                icon="el-icon-plus"
                size="small"
                @click="dialogVisible = true"
                >新增角色</el-button
              >
            </el-row>
            <el-table border="" :data="list" style="width: 100%">
              <el-table-column
                label="序号"
                width="120px"
                type="index"
                align="center"
              ></el-table-column>
              <el-table-column
                label="名称"
                width="240px"
                prop="name"
                align="center"
              ></el-table-column>
              <el-table-column
                label="描述"
                prop="description"
                align="center"
              ></el-table-column>
              <el-table-column label="操做" align="center">
                <template slot-scope="{ row }">
                  <el-button
                    type="success"
                    size="small"
                    @click="assignPerm(row.id)"
                    >分配权限</el-button
                  >
                  <el-button
                    type="primary"
                    size="small"
                    @click="editRole(row.id)"
                    >编辑</el-button
                  >
                  <el-button
                    type="danger"
                    size="small"
                    @click="deleteRole(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height:60px"
            >
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="page.page"
                :page-sizes="[5, 10, 20, 30]"
                :page-size="page.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="page.total"
                background
              >
              </el-pagination>
            </el-row>
          </el-tab-pane>
          <!-- 右边内容 -->
          <el-tab-pane label="公司信息" name="right">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              :closable="false"
              :show-icon="true"
            >
            </el-alert>
            <!-- 并不是所有的表单都需要 model rules -->
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="企业名称">
                <el-input
                  v-model="formData.name"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  disabled
                  style="width:400px"
                  v-model="formData.companyAddress"
                />
              </el-form-item>
              <el-form-item label="电话">
                <el-input
                  v-model="formData.companyPhone"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  disabled
                  style="width:400px"
                  v-model="formData.mailbox"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  disabled
                  style="width:400px"
                  type="textarea"
                  v-model="formData.remarks"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 放置一个弹层组件 -->
    <el-dialog title="编辑部门" :visible="dialogVisible" @close="btnCancel">
      <el-form
        label-width="120px"
        :model="roleForm"
        :rules="roleFormRules"
        ref="roleFormRef"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description"></el-input>
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="center">
        <el-col :span="8">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 分配权限弹层 -->
    <el-dialog title="分配权限" :visible="permissionDialogVisible" @close="btnPermCancel">
      <!-- 权限是一个颗树形结构 -->
      <!-- :check-strictly="true"  父子是否关联 为true 父子不互相关联   false 则关联 -->
      <!-- id作为唯一标识 -->
      <!-- :default-checked-keys="selectCheck" 这种冒号的形式，数据不是双向绑定的，组件无法将数据传递给model 所以我们得从其他方式获取数据 -->
      <el-tree
        :data="permData"
        :props="defaultProps"
        :default-expand-all="true"
        show-checkbox
        :check-strictly="true"
        node-key="id"
        :default-checked-keys="selectCheck"
        ref="permTree"
      >
      </el-tree>
      <el-row type="flex" justify="center" flot="footer">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnPermOk">确定</el-button>
          <el-button size="small" @click="btnPermCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRoleList,
  getCompanyInfo,
  deleteRole,
  getRoleDetail,
  updateRole,
  addRole,
  assignPerm
} from "@/api/setting";
import { getPermissionList } from "@/api/permission";
import { tranListToTreeData } from "@/utils";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      selectCheck: [], //用户记录当前权限点的标识
      roleId: null, //记录分配权限的id
      permData: [],
      defaultProps: {
        label: "name"
      },
      permissionDialogVisible: false,
      activeName: "left",
      list: [],
      page: {
        page: 1,
        pagesize: 2,
        total: 0
      },
      formData: {}, //公司信息
      dialogVisible: false,
      roleForm: {
        name: "",
        description: ""
      },
      roleFormRules: {
        name: [{ required: true, message: "角色名称不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getRoleList(), this.getCompanyInfo();
  },
  methods: {
    // 获取角色列表
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page);
      this.page.total = total;
      this.list = rows;
    },
    //根据id获取公司信息
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId);
    },
    //根据id删除角色
    async deleteRole(id) {
      //提示
      // 将pormise 改为 async  await 没法捕获错误,需要 try catch  final
      try {
        const confirmResult = await this.$confirm("确认删除该角色吗?");
        await deleteRole(id);
        this.getRoleList();
        this.$message.success("删除角色成功");
      } catch (error) {
        console.log(error);
      }
    },
    async editRole(id) {
      this.roleForm = await getRoleDetail(id);
      this.dialogVisible = true;
    },
    async btnOK() {
      try {
        if (this.roleForm.id) {
          //编辑
          await this.$refs.roleFormRef.validate(); //这里不传入回调函数,返回的是一个promise 对象,可以通过 .then  .catch 的形式 捕获成功 与 失败 也可以通过  async  await 的形式
          //也可以通过 try  catch 的形式捕获错误
          await updateRole(this.roleForm);
        } else {
          //新增
          await addRole(this.roleForm);
        }
        this.getRoleList();
        this.$message.success("操作成功");
        this.dialogVisible = false;
      } catch (error) {
        console.log(error);
      }
    },
    btnCancel() {
      this.roleForm = {
        name: "",
        description: ""
      };
      this.$refs.roleFormRef.resetFields();
      this.dialogVisible = false;
    },

    handleSizeChange(newPage) {
      this.page.pagesize = newPage;
      this.getRoleList();
    },
    handleCurrentChange(newPage) {
      this.page.page = newPage;
      this.getRoleList(this.page);
    },
    handleClick(tab, event) {},
    //弹出层  分配权限
    async assignPerm(id) {
      this.permData = tranListToTreeData(await getPermissionList(), "0"); //转化为数据行结构的数据
      // 有id 记录id
      this.roleId = id;
      const { permIds } = await getRoleDetail(id);  //当前角色所拥有的角色点id
      //将当前角色的角色点赋值
      this.selectCheck = permIds;
      this.permissionDialogVisible = true;
    },
    //确定分配权限
    btnPermOk(){
      //调用el-tree的方法 getCheckedKeys 则返回目前被选中的节点的 key 所组成的数组
      //调用接口更新角色权限
      assignPerm({permIds:this.$refs.permTree.getCheckedKeys(),id:this.roleId})
      this.$message.success("分配角色权限成功")
      this.permissionDialogVisible = false
    },
    //取消分配权限
    //取消的时候重置数据
    btnPermCancel(){
      this.selectCheck = []  //重置数据
      this.permissionDialogVisible = false
    }
  },
  computed: {
    ...mapGetters(["companyId"])
  }
};
</script>

<style></style>
